/*
 * @Description  : 脚手架配置工具
 * @Autor        : 杜康
 * @Date         : 2023-03-15 15:10:28 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2023-03-16 17:22:24 +0800
 */
import { isEmpty } from "lodash";
/**
 * @description: 转换数据格式
 * @param {*} data
 * @return {*}
 * @author: 杜康
 * @Date: 2023-03-15 15:43:43 +0800
 */
export const useEnv = (data) => {
  if (isEmpty(data)) return {};
  const params = {};
  const { keys, values, entries } = Object;
  const numKeys = ["VITE_PROT"];
  for (const [key, value] of entries(data)) {
    if (value === "true" || value === "false") {
      params[key] = value === "true" ? true : false;
    } else if (numKeys?.includes(key)) {
      params[key] = Number(value);
    } else {
      params[key] = value;
    }
  }
  return params;
};

/**
 * @description: 生成Proxy
 * @param {*} data
 * @return {*}
 * @author: 杜康
 * @Date: 2023-03-15 16:02:27 +0800
 */
export const proxy = (data) => {
  const { VITE_HTTPS, VITE_TARGET } = useEnv(data);
  const HTTP = VITE_HTTPS ? `https://` : `http://`;
  const WS = VITE_HTTPS ? `wss://` : `ws://`;
  return {
    "/api": {
      target: `${HTTP}${VITE_TARGET}`,
      changeOrigin: true
      // rewrite: (path) => path.replace(/^\/api/, "")
    },
    "/websocket": {
      target: `${WS}${VITE_TARGET}`,
      ws: VITE_HTTPS
    }
  };
};
